package com.lin.activity;

import java.util.ArrayList;

import com.lin.utils.Session;
import com.lin.utils.Session.ResultReceiver;
import com.lin.views.ListViewForSuperFriendsNearBy;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.ShareActivity;
import com.yunfeng.supercall.bean.SuperFriendsBean;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class EntrepriseNearByActivity extends Activity implements OnItemClickListener, OnClickListener{
	ListViewForSuperFriendsNearBy mListView;
	Session mSession;
	ArrayList<SuperFriendsBean> mList;
	private FrameLayout mFrameLayoutMore;
	private FrameLayout mFrameLayoutBack;
	int pagenum = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_entreprise_nearby);
		
		mList = new ArrayList<SuperFriendsBean>(); 
		
		mListView = (ListViewForSuperFriendsNearBy) findViewById(R.id.listview);
		
		mListView.setOnItemClickListener(this);

		mFrameLayoutMore = (FrameLayout) findViewById(R.id.framelayout_more);
		mFrameLayoutBack = (FrameLayout) findViewById(R.id.framelayout_back);
		
		mFrameLayoutBack.setOnClickListener(this);
		mFrameLayoutMore.setOnClickListener(this);
		mSession = Session.getSession();
		mSession.init(getApplicationContext(), true);
		load(pagenum);
		
		mListView.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				switch(scrollState) {
			case OnScrollListener.SCROLL_STATE_IDLE:
				// 判断滚动到底部
				if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
					pagenum++;
					load(pagenum);
					Toast.makeText(EntrepriseNearByActivity.this, "正在加载新数据", Toast.LENGTH_SHORT).show();
				}
				break;
			}	
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				
			}
		});
			}
	
	void load(int num) {
		mSession.GetEnSuperChatList(getApplicationContext() , pagenum, 113, 23, new ResultReceiver<ArrayList<SuperFriendsBean>>() {

			@Override
			public void onResult(boolean last, ArrayList<SuperFriendsBean> data) {
				// TODO Auto-generated method stub
				if(data == null) return;
				mList.addAll(data);
				mListView.setDataList(mList);
				
			}
			
		});

	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		// TODO Auto-generated method stub
		SuperFriendsBean mSuperFriendsBean = mList.get(position);
		Intent intent = new Intent(this, PersonalDetailCardActivity.class);
		intent.putExtra("data", mSuperFriendsBean);
		startActivity(intent);
	}
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		 if(mFrameLayoutBack == view) {
				finish();
			} else if(mFrameLayoutMore == view) {
				Intent intent = new Intent(this, ShareActivity.class);
				startActivity(intent);
			}
	}
	
	
	
}